/***********************************************************
 * Mapping management including target, host, hostGroup
 ***********************************************************/
/**
 * 
 */
var _hostId;
var _targetId;
var _groupId;
var Target = {
	
	/**
	 * Show list
	 */
	list : function() {
		// Request user list url.
		var targetListUrl = baseURL + "/mapping/target/list"
		$.ajax({
			type : "get",
			url : targetListUrl,
			dataType : "html",
			success : function(html) {
				// Request successfully and fill the data.
				$("#targetList").empty().html(html);
			}
		});
	},
	/**
	 * New target
	 */
	create : function() {
		_targetId = "";
		var title = localeMsg.target.create;
		common.dialog({
			obj : "#editTargetDialog",
			type : "get",
			url : baseURL + "/mapping/target/edit",
			title : title,
			width : 400,
			confirm : function() {
				if($("#editTargetForm").form('validate')!=true){
					return;
				}
				Target.save();
			}
		});
	},
	/**
	 * Edit target
	 */
	edit : function() {
		
		var targetId = $("#objectId").val();
		//alert(targetId);
		var title = localeMsg.target.create;
		var param = {};
		if (targetId && targetId != "") {
			_targetId = targetId;
			title = localeMsg.target.edit;
			param.targetId = targetId;
			common.dialog({
				obj : "#editTargetDialog",
				type : "get",
				url : baseURL + "/mapping/target/edit",
				param : param,
				title : title,
				width : 400,
				confirm : function() {
					if($("#editTargetForm").form('validate')!=true){
						return;
					}
					Target.save();
				}
			});
		}else{
			common.showTip({
				title : localeMsg.common.warn ,
				text : localeMsg.common.selectMsg
			});
		}
	},
	
	
	/**
	 * Show target details
	 */
	showTargetDetails : function() {
		var targetId = $("#objectId").val();
		var title = localeMsg.target.details;
		var param = {};
		if (targetId && targetId != "") {
			title = localeMsg.target.details;
			param.targetId = targetId;
			common.dialog({
				obj : "#editTargetDialog",
				type : "get",
				url : baseURL + "/mapping/target/showTargetDetails",
				param : param,
				title : title,
				width : 700,
				confirm : function() {
					
					Target.list();
				}
			});
		}else{
			common.showTip({
				title : localeMsg.common.warn ,
				text : localeMsg.common.selectMsg
			});
		}
		
	},
	
	/**
	 * Save pool
	 */
	save : function() {
		//alert("save");
		$("#editTargetForm").ajaxSubmit({
			success : function(json) {
				// Save successfully and refresh the user list.
				if (json.result && json.result == "success") {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.saveSuccess,
						success : true,
						callback : function() {
							//Target.list();
							//alert("cheng");
							window.location.reload(); 
						}
					});
				} else {
					common.showTip({title : localeMsg.common.warn, text : localeMsg.common.saveFailed});
				}
			}
		});
	},
	/**
	 * Remove targets
	 */
	removeTarget : function() {
		var id = $("#objectId").val();
		var targetIds=[];
		
		targetIds.push(id); 
		
		if(targetIds==""){
			common.showTip({title : localeMsg.common.warn, text : localeMsg.common.selectMsg});
		}else{
			common.confirmDialog(localeMsg.common.deleteConfirm, function() {
				$.ajax({
					type : "post",
					url : baseURL + "/mapping/target/remove",
					data : {targetIds : targetIds},
					traditional: true,
					dataType : "json",
					success : function(json) {
						if (json.result && json.result == "success") {
							common.showTip({
								title : localeMsg.common.prompt,
								text : localeMsg.common.deleteSuccess,
								success : true,
								callback : function() {
									window.location.reload(); 
								}
							});
						} else {
							common.showTip({title : localeMsg.common.warn, text : localeMsg.common.deleteFail});
						}
					}
				});
			});
		}
		
	},
	/**
	 * Remove hostGroups
	 */
	removeHostGroup : function() {
		var id = $("#objectId").val();
		var groupId=[];
		
		groupId.push(id); 
		
		$.ajax({
			type : "post",
			url : baseURL + "/mapping/hg/remove",
			data : {groupIds : groupId},
			traditional: true,
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.deleteSuccess,
						success : true,
						callback : function() {
							window.location.reload(); 
						}
					});
				} else {
					common.showTip({title : localeMsg.common.warn, text : localeMsg.common.deleteFailed});
				}
			}
		});
	},
	validate : function(){
		$('#targetName').validatebox({
			required : true,
			validType :	['vername','uniquename["/mapping/target/validateName","'+_targetId+'"]']
		});
		$('#targetIp').validatebox({
			required : true,
			validType : ['ip','uniqueip["/mapping/target/validateIp","'+_targetId+'"]']
		});
	},
	
	/**
	 * 显示hg列表
	 * @param element
	 * @param type 显示类型
	 */
	/*showGroups : function(element, type) {
		var targetId = $("#objectId").val();
		var url = baseURL + "/mapping/hg/list";
		var param = {targetId : targetId};
		if (type) {
			param.type = type;
		}
		common.dialog({
			type : "get",
			url : url,
			param : param,
			title : "Group List",
			width : 680,
			confirm : function(dialog) {
				var groupIdArr = [];
				$(dialog).find("input[name='groupId']:checked").each(function() {
					groupIdArr.push($(this).val());
				});
				Target.saveTargetGroups(targetId, groupIdArr,type);
			},
			onOpen : function() {
				if (type != "other") {
					$(".l-btn-selected span.l-btn-text").text("删除");
				}
			}
		});
	},*/
	
	
	showGroups : function(element, type) {
		var confirmText = localeMsg.common.confirm;
		var targetId = $("#objectId").val();
		var url = baseURL + "/mapping/hg/listNoAction";
		var param = {targetId : targetId};
		if (type) {
			param.type = type;
		} else {
			confirmText = localeMsg.common.deleted;
		}
		var dialog;
		if ($("#groupList").length < 1) {
			$("#listGroupForm").append('<div id="groupList" ></div>');
			dialog = "#groupList";
		}
		common.dialog({
			obj : dialog,
			title : localeMsg.group.list,
			width : 680,
			height : 540,
			confirm : function(dialog) {
				var groupIdArr = [];
				$(dialog).find("input[name='groupId']:checked").each(function() {
					groupIdArr.push($(this).val());
				});
				if(!(groupIdArr.length>0)) {
					return;
				}
				Target.saveTargetGroups(targetId, groupIdArr,type);
			},
			confirmText : confirmText,
			onOpen : function() {
				$("#listGroupForm").ajaxpager({
					target: "#groupList",
					callback: function() {
						Target.showGroupsByPager(targetId, type);
					}
				});
			}
		});
	},
	
	showGroupsByPager : function(targetId, type) {
		var pageNumber = $("#groupList input[name='pageNumber']").val();
		var url = baseURL + "/mapping/hg/listNoAction";
		var param = {targetId : targetId, pageNumber : pageNumber};
		if (type) {
			param.type = type;
		}
		$.ajax({
			type : "get",
			url : url,
			data : param,
			success : function(html) {
				$("#groupList").empty().html(html);
				$("#groupList").dialog("resize");
			}
		});
	},
	
	/**
	 * 保存target-hostGroup关系
	 * @param targetId
	 * @param groupIdArr
	 */
	saveTargetGroups : function(targetId, groupIdArr,type) {
		$.ajax({
			type : "post",
			url : baseURL + "/mapping/target/hg",
			traditional: true,
			data : {targetId: targetId, hgIds : groupIdArr,type :type},
			dataType : "json",
			success : function(json) {
				common.showTip({
					title : localeMsg.common.prompt,
					text : "Success",
					success : true,
					callback : function() {
						window.location.reload(); 
					}
				});
			}
		});
	},
};

/**
 * 
 */
var Group = {
	/**
	 * Show group list
	 */
	list : function() {
		// Request user list url.
		var groupListUrl = baseURL + "/mapping/hg/list"
		$.ajax({
			type : "get",
			url : groupListUrl,
			dataType : "html",
			success : function(html) {
				// Request successfully and fill the data.
				$("#hgList").empty().html(html);
			}
		});
	},
	
	/**
	 * New group
	 */
	create : function() {
		var title = localeMsg.group.create;
		_groupId = "";
		common.dialog({
			obj : "#editGroupDialog",
			type : "get",
			url : baseURL + "/mapping/hg/edit",
			title : title,
			width : 400,
			confirm : function() {
				if($("#editGroupForm").form('validate')!=true){
					return;
				}
				Group.save();
			}
		});
	},
	
	/**
	 * Remove HostGroups
	 */
	removeHostGroup : function() {
		var id = $("#objectId").val();
		var groupId=[];
		
		groupId.push(id); 
		
		if(groupId==""){
			common.showTip({title : localeMsg.common.warn, text : localeMsg.common.selectMsg});
		}else{
			common.confirmDialog(localeMsg.common.deleteConfirm, function() {
				$.ajax({
					type : "post",
					url : baseURL + "/mapping/hg/remove",
					data : {groupIds : groupId},
					traditional: true,
					dataType : "json",
					success : function(json) {
						if (json.result && json.result == "success") {
							common.showTip({
								title : localeMsg.common.prompt,
								text : localeMsg.common.deleteSuccess,
								success : true,
								callback : function() {
									window.location.reload(); 
								}
							});
						} else {
							common.showTip({title : localeMsg.common.warn, text : localeMsg.common.deleteFail});
						}
					}
				});
			});
		}
		
	},
	/**
	 * Edit group
	 */
	edit : function() {
		var groupId = $("#objectId").val();
		var title = localeMsg.group.create;
		var param = {};
		if (groupId && groupId != "") {
			_groupId = groupId;
			title = localeMsg.group.edit;
			param.groupId = groupId;
			common.dialog({
				obj : "#editGroupDialog",
				type : "get",
				url : baseURL + "/mapping/hg/edit",
				param : param,
				title : title,
				width : 400,
				confirm : function() {
					if($("#editGroupForm").form('validate')!=true){
						return;
					}
					Group.save();
				}
			});
		}else{
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}
	},
	
	/**
	 * Show HostGroup details
	 */
	showHostGroupDetails : function() {
		var targetId = $("#objectId").val();
		//var mappingName=$("#mappingName").val();
		
		var title = localeMsg.group.details;
		var param = {};
		if (targetId && targetId != "") {
			title = localeMsg.group.details;
			param.targetId = targetId;
			//param.mappingName = mappingName;
			common.dialog({
				obj : "#editTargetDialog",
				type : "get",
				url : baseURL + "/mapping/hg/showHostGroupDetails",
				param : param,
				title : title,
				width : 700,
				confirm : function() {
					
					Group.list();
				}
			});
		}else{
			common.showTip({
				title : localeMsg.common.warn ,
				text : localeMsg.common.selectMsg
			});
		}
		
	},
	
	/**
	 * Save group
	 */
	save : function() {
		var groupId = $("#editGroupForm input[id='id']").eq(0).val();
		var groupName = $("#editGroupForm input[id='groupName']").eq(0).val();
		var chap= $("input:radio[name='chap']:checked").val();
		var inuser=$("#incoming_user").val();
		var inpassword=$("#incoming_password").val();
		var outuser=$("#outgoing_user").val();
		var outpassword=$("#outgoing_password").val();
		
		var saveUrl = baseURL + "/mapping/hg/save";
		$.ajax({
			type : "post",
			url : saveUrl,
			data : {
					groupId : groupId , 
					groupName : groupName,
					chap:chap,
					inuser:inuser,
					inpassword:inpassword,
					outuser:outuser,
					outpassword:outpassword
					},
			dataType : "json",
			success : function(json) {
				if(json.data && json.data == "0"){
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.exit,
				});
				}else{
					if (json.result && json.result == "success") {
						common.showTip({
							title : localeMsg.common.prompt,
							text : localeMsg.common.saveSuccess,
							//对错的提示图标
							success : true,
							callback : function() {
								window.location.reload();
							}
						});
					} else {
						common.showTip({title : localeMsg.common.warn, text : localeMsg.common.saveFail});
					}
				}
			}
		})
	},
	
	
	
	
	/**
	 * Remove groups confirm
	 */
	remove : function(){
		var roleId=[];
		$('input[name="groupId"]:checked').each(function(){
			roleId.push($(this).val()); 
            });  
		if (Object.keys(roleId).length === 0){
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}else{
			var confirmMessage = localeMsg.common.relateddeleteconfirm
				common.confirmDialog(confirmMessage,Group.removeDel);
		}

	},
	
	
	/**
	 * Remove groups
	 */
	removeDel : function() {
		//  Remove  records
		var groupId=[];
		$('input[name="groupId"]:checked').each(function(){
			groupId.push($(this).val()); 
        });  
		
		$.ajax({
			type : "post",
			url : baseURL + "/mapping/hg/remove",
			data : {groupIds : groupId},
			traditional: true,
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.deleteSuccess,
						success : true,
						callback : function() {
							window.location.reload(); 
						}
					});
				} else {
					common.showTip({title : localeMsg.common.warn, text : localeMsg.common.deleteFailed});
				}
			}
		});
	},
	
	validate : function(){
	    $('#groupName').validatebox({
	        required: true,
	        validType :	['vername','uniquename["/mapping/hg/validateName","'+_groupId+'"]']
	    });
	    
	    var value = $("input:radio[name='chap']:checked").val();
		if(value==0){
			$(".incoming").css('display','none');
			$(".outgoing").css('display','none');
		}
		if(value==1){
			$(".incoming").css('display','');
			$(".outgoing").css('display','none');
			$('#incoming_user').validatebox({
		        required: true,
		        validType :	['vername']
			});
			$('#incoming_password').validatebox({
		        required: true,
			});
		}
		if(value==2){
			$(".incoming").css('display','');
			$(".outgoing").css('display','');
			$('#incoming_user').validatebox({
		        required: true,
		        validType :	['vername']
			});
			$('#incoming_password').validatebox({
		        required: true,
			});
			$('#outgoing_user').validatebox({
		        required: true,
		        validType :	['vername']
			});
			$('#outgoing_password').validatebox({
		        required: true,
			});
		}
	},
	
	
	/**
	 * 显示target选项
	 * @param e
	 */
	showTargets : function(element) {
		$.ajax({
			type : "get",
			url : baseURL + "/mapping/target/list.json",
			dataType : "json",
			success : function(json) {
				for (var k in json) {
					$(element).append('<option value="' + json[k].id + '">' + json[k].name + '</option>');
				}
			}
		});
	},
	
	/**
	 * 显示host列表
	 * @param element
	 * @param type 显示类型
	 */
	/*showHosts : function(element, type) {
		var groupId = $("#objectId").val();
		var url = baseURL + "/mapping/host/list";
		var param = {groupId : groupId};
		if (type) {
			param.type = type;
		}
		common.dialog({
			type : "get",
			url : url,
			param : param,
			title : "Add Hosts",
			width : 600,
			confirm : function(dialog) {
				var hostIdArr = [];
				$(dialog).find("input[name='hostId']:checked").each(function() {
					hostIdArr.push($(this).val());
				});
				Group.saveGroupHosts(groupId, hostIdArr,type);
			},
			onOpen : function() {
				if (type != "other") {
					$(".l-btn-selected span.l-btn-text").text("删除");
				}
			}
		});
	},*/
	
	showHosts : function(element, type) {
		var confirmText = localeMsg.common.confirm;
		var groupId = $("#objectId").val();
		var title = localeMsg.host.list;
		var url = baseURL + "/mapping/host/list";
		var param = {groupId : groupId};
		if (type) {
			param.type = type;
			title = localeMsg.host.add;
		} else {
			confirmText = localeMsg.common.deleted;
		}
		var dialog;
		if ($("#hostList").length < 1) {
			$("#listHostForm").append('<div id="hostList"></div>');
			dialog = "#hostList";
		}
		common.dialog({
			obj : dialog,
			title : title,
			width : 600,
			height : 540,
			confirm : function(dialog) {
				var hostIdArr = [];
				$(dialog).find("input[name='hostId']:checked").each(function() {
					hostIdArr.push($(this).val());
				});
				if(!(hostIdArr.length>0)) {
					return;
				}
				Group.saveGroupHosts(groupId, hostIdArr,type);
			},
			confirmText : confirmText,
			onOpen : function() {
				$("#listHostForm").ajaxpager({
					target: "#hostList",
					callback: function() {
						Group.showHostsByPager(groupId, type);
					}
				});
			}
		});
	},
	showHostsByPager : function(groupId, type) {
		var pageNumber = $("#hostList input[name='pageNumber']").val();
		var url = baseURL + "/mapping/host/list";
		var param = {groupId : groupId, pageNumber : pageNumber};
		if (type) {
			param.type = type;
		}
		$.ajax({
			type : "get",
			url : url,
			data : param,
			success : function(html) {
				$("#hostList").empty().html(html);
				$("#hostList").dialog("resize");
			}
		});
	},
	/**
	 * 保存group-host关系
	 * @param groupId
	 * @param hostIdArr
	 */
	/*saveGroupHosts : function(groupId, hostIdArr,type) {
		$.ajax({
			type : "post",
			url : baseURL + "/mapping/hg/hosts",
			traditional: true,
			data : {groupId: groupId, hostIds : hostIdArr, type : type},
			dataType : "json",
			success : function(json) {
				common.showTip({
					title : localeMsg.common.prompt,
					text : localeMsg.common.saveSuccess,
					success : true
				});
			}
		});
	},*/
	saveGroupHosts : function(groupId, hostIdArr,type) {
		$.ajax({
			type : "post",
			url : baseURL + "/mapping/hg/hosts",
			traditional: true,
			data : {groupId: groupId, hostIds : hostIdArr, type : type},
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.saveSuccess,
						success: true,
						callback : function() {
							window.location.reload();
						}
					});
				}else if(json.result && json.result == "wrong"){
					var names = "";
					for(var i=0; i<json.hostNames.length; i++)  
                       {  
                        names+= json.hostNames[i]+",";
                       }  
					names = names.substr(0,names.length - 1);
					common.showTip({
						title : localeMsg.common.warn,
						text : names+localeMsg.group.not+json.lunName+localeMsg.group.mapping,
						success: false
					});
				}else {
					common.showTip({
						title : localeMsg.common.warn,
						text : localeMsg.common.saveFailed,
						success: false
					});
				}
				
				
				/*common.showTip({
					title : localeMsg.common.prompt,
					text : localeMsg.common.saveSuccess,
					success : true
				});*/
			}
		});
	},
	
	/**
	 * 显示未与指定hostgroup做映射的lun列表
	 * @param element
	 */
	/*showLuns : function(element) {
		var groupId = $("#objectId").val();
		var url = baseURL + "/mapping/luns";
		var param = {groupId : groupId};
		if(groupId == undefined){
			common.showTip({
				title : localeMsg.common.prompt,
				text : localeMsg.common.selectMsg
			});
		}else{
			common.dialog({
				type : "get",
				url : url,
				param : param,
				title : "Lun List",
				width : 600,
				confirm : function(dialog) {
					var lunArr = [];
					$(dialog).find("input[name='lunId']:checked").each(function() {
						lunArr.push($(this).val());
					});
					Group.mapping(groupId, lunArr);
				}
			});
		}
	},*/
	showLuns : function(element) {
		var confirmText = localeMsg.common.confirm;
		var groupId = $("#objectId").val();
		var title = localeMsg.lun.add;
		var url = baseURL + "/mapping/luns";
		var param = {groupId : groupId};
		/*if (type) {
			param.type = type;
			title = localeMsg.host.add;
		} else {
			confirmText = localeMsg.common.deleted;
		}*/
		var dialog;
		if ($("#lunList").length < 1) {
			$("#listLunForm").append('<div id="lunList"></div>');
			dialog = "#lunList";
		}
		if(groupId == undefined){
			common.showTip({
				title : localeMsg.common.prompt,
				text : localeMsg.common.selectMsg
			});
		}else{
			common.dialog({
				obj : dialog,
				title : title,
				width : 600,
				height : 540,
				confirm : function(dialog) {
					var lunIdArr = [];
					$(dialog).find("input[name='lunId']:checked").each(function() {
						lunIdArr.push($(this).val());
					});
					if(!(lunIdArr.length>0)) {
						return;
					}
					Group.mapping(groupId, lunIdArr);
				},
				confirmText : confirmText,
				onOpen : function() {
					$("#listLunForm").ajaxpager({
						target: "#lunList",
						callback: function() {
							Group.showlunsByPager(groupId);
						}
					});
				}
			});
		}
	},
	showlunsByPager : function(groupId) {
		var pageNumber = $("#lunList input[name='pageNumber']").val();
		var url = baseURL + "/mapping/luns";
		var param = {groupId : groupId, pageNumber : pageNumber};
		
		$.ajax({
			type : "get",
			url : url,
			data : param,
			success : function(html) {
				$("#lunList").empty().html(html);
				$("#lunList").dialog("resize");
			}
		});
	},
	/*
	showHosts : function(element, type) {
		var confirmText = localeMsg.common.confirm;
		var groupId = $("#objectId").val();
		var title = localeMsg.host.list;
		var url = baseURL + "/mapping/host/list";
		var param = {groupId : groupId};
		if (type) {
			param.type = type;
			title = localeMsg.host.add;
		} else {
			confirmText = localeMsg.common.deleted;
		}
		var dialog;
		if ($("#hostList").length < 1) {
			$("#listHostForm").append('<div id="hostList"></div>');
			dialog = "#hostList";
		}
		common.dialog({
			obj : dialog,
			title : title,
			width : 600,
			confirm : function(dialog) {
				var hostIdArr = [];
				$(dialog).find("input[name='hostId']:checked").each(function() {
					hostIdArr.push($(this).val());
				});
				if(!(hostIdArr.length>0)) {
					return;
				}
				Group.saveGroupHosts(groupId, hostIdArr,type);
			},
			confirmText : confirmText,
			onOpen : function() {
				$("#listHostForm").ajaxpager({
					target: "#hostList",
					callback: function() {
						Group.showHostsByPager(groupId, type);
					}
				});
			}
		});
	},
	showHostsByPager : function(groupId, type) {
		var pageNumber = $("#hostList input[name='pageNumber']").val();
		var url = baseURL + "/mapping/host/list";
		var param = {groupId : groupId, pageNumber : pageNumber};
		if (type) {
			param.type = type;
		}
		$.ajax({
			type : "get",
			url : url,
			data : param,
			success : function(html) {
				$("#hostList").empty().html(html);
				//$("#hostList").dialog("resize");
			}
		});
	},*/
	/**
	 * 查看映射信息
	 * @param element
	 */
	/*showMappings : function() {
		var groupId = $("#hgList input[name='groupId']:checked").val();
		var url = baseURL + "/mapping/list";
		var param = {groupId : groupId};
		if(groupId == undefined){
			common.showTip({
				title : localeMsg.common.prompt,
				text : localeMsg.common.selectMsg
			});
		}else{
			common.dialog({
				type : "get",
				url : url,
				param : param,
				title : "Mapping List",
				width : 600,
				confirm : function(dialog) {
					var lunArr = [];
					$(dialog).find("input[name='lunId']:checked").each(function() {
						lunArr.push($(this).val());
					});
					Group.unmapping(groupId, lunArr);
				}
			});
		}
	},*/
	
	/*showMappings : function() {
		var groupId = $("#objectId").val();
		var url = baseURL + "/mapping/list";
		var param = {groupId : groupId};
		if(groupId == undefined){
			common.showTip({
				title : localeMsg.common.prompt,
				text : localeMsg.common.selectMsg
			});
		}else{
			common.dialog({
				type : "get",
				url : url,
				param : param,
				title : localeMsg.lun.mapping,
				width : 600,
				height : 480,
				confirm : function(dialog) {
					var lunArr = [];
					$(dialog).find("input[name='lunId']:checked").each(function() {
						lunArr.push($(this).val());
					});
					if(!(lunArr.length>0)) {
						return;
					}
					Group.unmapping(groupId, lunArr);
				},
				confirmText : localeMsg.common.deleted
			});
		}
	},*/
	
	showMappings : function(element) {
		var confirmText = localeMsg.common.confirm;
		var groupId = $("#objectId").val();
		var title = localeMsg.lun.list;
		var url = baseURL + "/mapping/list";
		var param = {groupId : groupId};
		/*if (type) {
			param.type = type;
			title = localeMsg.host.add;
		} else {
			confirmText = localeMsg.common.deleted;
		}*/
		var dialog;
		if ($("#mappedLunList").length < 1) {
			$("#listMappedLunForm").append('<div id="mappedLunList"></div>');
			dialog = "#mappedLunList";
		}
		if(groupId == undefined){
			common.showTip({
				title : localeMsg.common.prompt,
				text : localeMsg.common.selectMsg
			});
		}else{
			common.dialog({
				obj : dialog,
				title : title,
				width : 600,
				height : 540,
				confirm : function(dialog) {
					var lunIdArr = [];
					$(dialog).find("input[name='lunId']:checked").each(function() {
						lunIdArr.push($(this).val());
					});
					if(!(lunIdArr.length>0)) {
						return;
					}
					Group.unmapping(groupId, lunIdArr);
				},
				confirmText : confirmText,
				onOpen : function() {
					$("#listMappedLunForm").ajaxpager({
						target: "#mappedLunList",
						callback: function() {
							Group.showMappedlunsByPager(groupId);
						}
					});
				}
			});
		}
	},
	showMappedlunsByPager : function(groupId) {
		var pageNumber = $("#mappedLunList input[name='pageNumber']").val();
		var url = baseURL + "/mapping/list";
		var type = "tuopu";
		var param = {groupId : groupId,type:type,pageNumber : pageNumber};
		
		$.ajax({
			type : "get",
			url : url,
			data : param,
			success : function(html) {
				$("#mappedLunList").empty().html(html);
				$("#mappedLunList").dialog("resize");
			}
		});
	},
	
	
	/**
	 * 映射hostGroup-lun
	 * @param groupId
	 * @param lunArr
	 */
	/*mapping : function(groupId, lunArr) {
		$.ajax({
			type : "post",
			url : baseURL + "/mapping",
			traditional: true,
			data : {groupId: groupId, lunIds : lunArr},
			dataType : "json",
			success : function(json) {
				common.showTip({
					title : localeMsg.common.prompt,
					text : localeMsg.common.saveSuccess,
					success : true
				});
			}
		});
	},*/
	mapping : function(groupId, lunArr) {
		$.ajax({
			type : "post",
			url : baseURL + "/mapping",
			traditional: true,
			data : {groupId: groupId, lunIds : lunArr},
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.saveSuccess,
						success: true,
						callback : function() {
							window.location.reload(); 
						}
					});
				}else if(json.result && json.result == "wrong"){
					common.showTip({
						title : localeMsg.common.warn,
						text : localeMsg.lun.acl,
						success: false
					});
				}else {
					common.showTip({
						title : localeMsg.common.warn,
						text : localeMsg.common.saveFailed,
						success: false
					});
				}
				
				/*common.showTip({
					title : localeMsg.common.prompt,
					text : localeMsg.common.saveSuccess,
					success : true
				});*/
			}
		});
	},
	/**
	 * 解除映射
	 * @param groupId
	 * @param lunArr
	 */
	unmapping : function(groupId, lunArr) {
		$.ajax({
			type : "post",
			url : baseURL + "/mapping/delete",
			traditional: true,
			data : {groupId: groupId, ids : lunArr},
			dataType : "json",
			success : function(json) {
				common.showTip({
					title : localeMsg.common.prompt,
					text : localeMsg.common.saveSuccess,
					success : true,
					callback : function() {
						window.location.reload(); 
					}
				});
			}
		});
	},
}

/**
 * 
 */
var Host = {
	/**
	 * Show host list
	 */
	list : function() {
		// Request host list url.
		var hostListUrl = baseURL + "/mapping/host/list"
		$.ajax({
			type : "get",
			url : hostListUrl,
			dataType : "html",
			success : function(html) {
				$("#hostList").empty().html(html);
			}
		});
	},
	/**
	 * New host
	 */
	create : function() {
		var title = localeMsg.host.create;
		_hostId = "";
		common.dialog({
			obj : "#editHostDialog",
			type : "get",
			url : baseURL + "/mapping/host/edit",
			/*param : param,*/
			title : title,
			width : 400,
			confirm : function() {
				if($("#editHostForm").form('validate')!=true){
					return;
				}
				Host.save();
			}
		});
	},
	/**
	 * Show host details
	 */
	showHostDetails : function() {
		var hostId = $("#objectId").val();
		//var mappingName=$("#mappingName").val();
		
		var title = localeMsg.host.details;
		var param = {};
		if (hostId && hostId != "") {
			title = localeMsg.host.details;
			param.hostId = hostId;
			//param.mappingName = mappingName;
			common.dialog({
				obj : "#editTargetDialog",
				type : "get",
				url : baseURL + "/mapping/host/showHostDetails",
				param : param,
				title : title,
				width : 700,
				confirm : function() {
					
					Host.list();
				}
			});
		}else{
			common.showTip({
				title : localeMsg.common.warn ,
				text : localeMsg.common.selectMsg
			});
		}
		
	},
	/**
	 * Remove Host
	 */
	removeHost : function() {
		var id = $("#objectId").val();
		var hostIds=[];
		
		hostIds.push(id); 
		
		if(hostIds==""){
			common.showTip({title : localeMsg.common.warn, text : localeMsg.common.selectMsg});
		}else{
			common.confirmDialog(localeMsg.common.deleteConfirm, function() {
				$.ajax({
					type : "post",
					url : baseURL + "/mapping/host/remove",
					data : {hostIds : hostIds},
					traditional: true,
					dataType : "json",
					success : function(json) {
						if (json.result && json.result == "success") {
							common.showTip({
								title : localeMsg.common.prompt,
								text : localeMsg.common.deleteSuccess,
								success : true,
								callback : function() {
									window.location.reload(); 
								}
							});
						} else {
							common.showTip({title : localeMsg.common.warn, text : localeMsg.common.deleteFail});
						}
					}
				});
			});
		}
		
	},
	/**
	 *Edit host
	 */
	edit : function() {
		var hostId = $("#objectId").val();
		
		var title = localeMsg.host.edit;
		var param = {};
		if (hostId && hostId != "") {
			_hostId = hostId;
			title = localeMsg.host.edit;
			param.hostId = hostId;
			common.dialog({
				obj : "#editGroupDialog",
				type : "get",
				url : baseURL + "/mapping/host/edit",
				param : param,
				title : title,
				width : 400,
				confirm : function() {
					if($("#editGroupForm").form('validate')!=true){
						return;
					}
					Host.save();
				}
			});
		}else{
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}
	},
	/**
	 * Save host
	 */
	save : function() {
		$("#editHostForm").ajaxSubmit({
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.saveSuccess ,
						success : true,
						callback : function() {
							window.location.reload(); 
						}
					});
				} else {
					common.showTip({title : localeMsg.common.warn, text : localeMsg.common.saveFailed});
				}
			}
		});
	},
	
	/**
	 * Remove host
	 */
	/*removeHost : function() {
		var id = $("#objectId").val();
		var hostId=[];
		
		hostId.push(id); 
		
		$.ajax({
			type : "post",
			url : baseURL + "/mapping/host/remove",
			data : {hostId : hostId},
			traditional: true,
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						title : localeMsg.common.prompt,
						text : localeMsg.common.deleteSuccess,
						success : true,
						callback : function() {
							Host.list();
						}
					});
				} else {
					common.showTip({title : localeMsg.common.warn, text : localeMsg.common.deleteFailed});
				}
			}
		});
	},
	*/
	
	remove : function(){
		var hostId=[];
		$('input[name="hostId"]:checked').each(function(){
			hostId.push($(this).val()); 
            });  
		if (Object.keys(hostId).length === 0){
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg 
			});
		}else{
			var confirmMessage = localeMsg.common.deleteConfirm
				common.confirmDialog(confirmMessage,Host.removeDel);
		}

	},
	/**
	 * Remove hosts
	 */
	removeDel : function() {
	
			var hostIds=[];
			$('input[name="hostId"]:checked').each(function(){
				hostIds.push($(this).val()); 
			});  
			
			$.ajax({
				type : "post",
				url : baseURL + "/mapping/host/remove",
				data : {hostIds : hostIds},
				traditional: true,
				dataType : "json",
				success : function(json) {
					if (json.result && json.result == "success") {
						common.showTip({
							title : localeMsg.common.prompt,
							text : localeMsg.common.deleteSuccess ,
							success : true,
							callback : function() {
								window.location.reload(); 
							}
						});
					} else {
						common.showTip({title : localeMsg.common.warn, text : localeMsg.common.deleteFail});
					}
				}
			});
		
	},
	validate : function(){
		$('#hostName').validatebox({
			required : true,
			validType :	['vername','uniquename["/mapping/host/validateName", "'+_hostId+'"]']
		});
		$('#ipAddress').validatebox({
			required : true,
			validType : ['ip','uniqueip["/mapping/host/validateIp", "'+_hostId+'"]']
		});
	},
};